﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace SHKF.B2C.JuShuiTan.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        public async Task Get()
        {
            StringBuilder htmlStringBuilder = new StringBuilder();
            htmlStringBuilder.Append("<html>");
            htmlStringBuilder.Append("<head><meta http-equiv=\"Content-Type\" content=\"charset=utf-8\" />");//支持中文
            htmlStringBuilder.Append("<style>");
            htmlStringBuilder.Append(".area { margin-left:16px; margin-top:16px; margin-bottom:20px;}");
            htmlStringBuilder.Append(".table { margin-left:16px; }");
            htmlStringBuilder.Append("td { border-bottom: 1px solid #808080;padding:8px }");
            htmlStringBuilder.Append("</style>");
            htmlStringBuilder.Append("<body>");
            htmlStringBuilder.Append("<ol style=\"font - size: 300 % \">");//让字体变大　
            string datahtml = $"JuHuiTan hello";

            List<string> list = new List<string>() { };
            list.Add(GetList());           //http://43.138.6.61:5002/order/list
            list.Add(ProductUpload());     //http://43.138.6.61:5002/product/upload
            list.Add(InventoryUpload());   //http://43.138.6.61:5002/inventory/upload
             
            list.ForEach(x =>
            {
                datahtml += "<li style='margin: 20px;color:blue;margin-top:32px'>" + x + "</li>";
            });

            htmlStringBuilder.Append(datahtml);
            htmlStringBuilder.Append("</ol>");
            htmlStringBuilder.Append("</body>");
            htmlStringBuilder.Append("</html>");
            string result = htmlStringBuilder.ToString();
            var data = Encoding.UTF8.GetBytes(result);

            Response.ContentType = "text/html";

            await Response.Body.WriteAsync(data, 0, data.Length);
        }

        private string GetList()
        {
            string html = "获取线上出库订单 Post: http://43.138.6.61:5002/order/list <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "OrdersSingleParam", ColumnType = "类", Remark = "&nbsp;" }); 
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "OrderDto", ColumnType = "类", Remark = "&nbsp;" });

            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }

        private string ProductUpload()
        {
            string html = "上架产品 Post: http://43.138.6.61:5002/product/upload <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "ItemSkuParam", ColumnType = "类", Remark = "&nbsp;" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "BaseDto", ColumnType = "类", Remark = "&nbsp;" });

            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }

        private string InventoryUpload()
        {
            string html = "新建盘点单 Post: http://43.138.6.61:5002/inventory/upload <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "InventoryUploadParam", ColumnType = "类", Remark = "&nbsp;" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "InventoryUploadDto", ColumnType = "类", Remark = "&nbsp;" });

            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }


        private string GetIn(List<Row> columns)
        {
            string html = "<div class=\"area\">入参:" +
                "<table class=\"table\" cellpadding=\"0\" cellspacing=\"0\">";
            html += this.GetColumnHeader();

            foreach (Row column in columns)
            {
                html += this.GetRow(column.ColumnName, column.ColumnType, column.Remark);
            }

            html += "</table></div>";
            return html;
        }
        private string GetOut(List<Row> columns)
        {
            string html = "<div class=\"area\">出参:" +
                "<table class=\"table\" cellpadding=\"0\" cellspacing=\"0\">";
            html += this.GetColumnHeader();

            foreach (Row column in columns)
            {
                html += this.GetRow(column.ColumnName, column.ColumnType, column.Remark);
            }

            html += "</table></div>";
            return html;
        }
        private string GetColumnHeader()
        {
            return $"<tr>" +
                $"<td class=\"header\">列名</td>" +
                $"<td class=\"header\">类型</td>" +
                $"<td class=\"header\">备注</td>" +
                $"</tr>";
        }
        private string GetRow(string columnName, string strType, string remark)
        {
            return $"<tr>" +
                $"<td>{columnName}</td>" +
                $"<td>{strType}</td>" +
                $"<td>{remark}</td>" +
                $"</tr>";
        }
    }
}
